﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Police_TranslateDocument_TranslateIntroFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["BaskUrl"] = Request.UrlReferrer.ToString();
            if (Request["id"] != null)
            {
                //string path = Server.MapPath(".\\WordFile\\");
                caption.Text = "修改请示文件";
                TranslateIntroFileInfo info = TranslateIntrolFile.GetDetail(new Guid(Request.QueryString["id"]));
                HyperLink1.NavigateUrl = "./WordFile/" + info.PilePath;
            }
            else
            {
                caption.Text = "上传介绍信文件";
                HyperLink1.Visible = false;
            }


        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(".\\WordFile\\");
        if (Request["id"] == null)
        {
            string word = "";

            TranslateIntroFileInfo info = new TranslateIntroFileInfo();
            if (WordFile.HasFile)
            {
                string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
                if (ex.ToLower() != "jpg" && ex.ToLower() != "bmp" && ex.ToLower() != "gif")
                {
                    Prompt.Text = "提示:上传必须为图片!";
                    return;
                }
                else
                {
                    if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                    {
                        Prompt.Text = "提示:上传文档不可大于5M!";
                        return;

                    }
                    else
                    {

                        word = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + ex;

                        WordFile.SaveAs(path + word);
                        info.DateBook = DateTime.Now;

                        info.PilePath = word;
                        info.Bookmen = User.Identity.Name;
                        Guid id = TranslateIntrolFile.Insert(info);
                        if (Request.QueryString["Fid"] != null)
                        {
                            TranslateInfo TranslateInfo = Translate.GetDetail(new Guid(Request.QueryString["Fid"]));
                            TranslateLogInfo log = new TranslateLogInfo();
                            log.DID = id;
                            log.FID = TranslateInfo.ID;
                            log.LastTime = DateTime.Now;
                            log.PID = TranslateProcess.GetNextDetail(TranslateLogs.GetLastDetail(TranslateInfo.ID).PID).ID;
                            TranslateLogs.Insert(log);
                            if (TranslateInfo.TID == 1)
                            {
                                Response.Redirect("NewSoldier.aspx?Fid=" + Request.QueryString["Fid"]);
                            }
                            else
                            {
                                Response.Redirect("IntroduceLetterNew.aspx?Fid=" + Request.QueryString["Fid"]);
                            }
                        }
                    }
                }

            }
            else
            {
                info.DateBook = DateTime.Now;

                info.PilePath = word;
                info.Bookmen = User.Identity.Name;
                Guid id = TranslateIntrolFile.Insert(info);
                if (Request.QueryString["Fid"] != null)
                {
                    TranslateInfo TranslateInfo = Translate.GetDetail(new Guid(Request.QueryString["Fid"]));
                    TranslateLogInfo log = new TranslateLogInfo();
                    log.DID = id;
                    log.FID = TranslateInfo.ID;
                    log.LastTime = DateTime.Now;
                    log.PID = TranslateProcess.GetNextDetail(TranslateLogs.GetLastDetail(TranslateInfo.ID).PID).ID;
                    TranslateLogs.Insert(log);
                    if (TranslateInfo.TID == 1)
                    {
                        Response.Redirect("NewSoldier.aspx?Fid=" + Request.QueryString["Fid"]);
                    }
                    else
                    {
                        Response.Redirect("IntroduceLetterNew.aspx?Fid=" + Request.QueryString["Fid"]);
                    }
                }
            }
        }
        else
        {
            string word = "";

            TranslateIntroFileInfo info = TranslateIntrolFile.GetDetail(new Guid(Request.QueryString["id"]));
            if (WordFile.HasFile)
            {
                string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
                if (ex.ToLower() != "jpg" && ex.ToLower() != "bmp" && ex.ToLower() != "gif")
                {
                    Prompt.Text = "提示:上传必须为图片!";
                    return;
                }
                else
                {
                    if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                    {
                        Prompt.Text = "提示:上传文档不可大于5M!";
                        return;

                    }
                    else
                    {
                        if (info.PilePath != "")
                        {
                            word = info.PilePath;
                            System.IO.FileInfo file = new System.IO.FileInfo(path + word);
                            if (file.Exists)
                            {
                                file.IsReadOnly = false;
                            }
                        }
                        else
                        {
                            word = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + ex;
                        }
                        WordFile.SaveAs(path + word);
                    }
                }

            }
            if (info.PilePath != "")
            {
                word = info.PilePath;
            }

            info.DateBook = DateTime.Now;
            info.PilePath = word;
            info.Bookmen = User.Identity.Name;
            TranslateIntrolFile.Update(info);
            Prompt.Text = "提示:修改请示文件成功!";
        }
    }
    protected void lkbBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["BaskUrl"].ToString());
    }
}
